package cdu.sl.controller;


import cdu.sl.anno.OperateLog;
import cdu.sl.pojo.PageResult;
import cdu.sl.pojo.Result;
import cdu.sl.pojo.StuQueryParam;
import cdu.sl.pojo.Student;
import cdu.sl.service.StudentService;
import org.apache.ibatis.annotations.Param;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/students")
@RestController
public class StudentController {
    private static final Logger log = LoggerFactory.getLogger(StudentController.class);

    @Autowired
    private StudentService studentService;

    /**
     * 分页查询员工信息
     *
     * @param stuQueryParam
     * @return
     */
    @GetMapping()
    public Result page(StuQueryParam stuQueryParam) {
        log.info("分页查询学生信息,{}", stuQueryParam);
        PageResult<Student> pageResult = studentService.page(stuQueryParam);
        return Result.success(pageResult);
    }

    /**
     * 批量删除学生
     *
     * @param ids
     * @return
     */
    @OperateLog
    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable List<Integer> ids) {
        log.info("批量删除学员信息:{}", ids);
        studentService.delete(ids);
        return Result.success();
    }

    /**
     * 添加学生
     *
     * @param student
     * @return
     */
    @OperateLog
    @PostMapping()
    public Result add(@RequestBody Student student) {
        log.info("新增学生信息：{}", student);
        studentService.add(student);
        return Result.success();
    }

    /**
     * 根据id获取学员信息
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result getInfo(@PathVariable Integer id) {
        log.info("根据id获取学员信息：{}", id);
        Student student = studentService.getInfo(id);
        return Result.success(student);
    }

    /**
     * 修改学生信息
     *
     * @param student
     * @return
     */
    @OperateLog
    @PutMapping()
    public Result update(@RequestBody Student student) {
        log.info("修改学生信息：{}", student);
        studentService.update(student);
        return Result.success();
    }

    @PutMapping("/violation/{id}/{score}")
    public Result updateViolationInfo(@PathVariable Integer id, @PathVariable Integer score) {
        log.info("根据id：{} 扣除学生分数：{}", id, score);
        studentService.updateViolationInfo(id,score);
        return Result.success();
    }


}
